package cn.fzkj.utils;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.T;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @DESCRIPTION: TODO excel工具类
 * @AUTHOR: AnotherOne
 * @DATE: 2021/9/10 14:33
 */
public class ExcelUtils {

    public static void main(String[] args) throws IOException {
//        exportExcel(null, "名字");
        File file = new File("F:/test.xls");
        file.delete();
    }

    public static String exportExcel(String sheetName){
        try {
            exportExcel(null, sheetName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 模拟返回文件名
        return "test.xls";
    }

    public static void exportExcel(List<T> datas, String sheetName) throws IOException {
        String[] titles = {"编号","名字", "昵称", "类型", "邮件"};

        /**
         * 先写入 标题栏数据
         */
//        1.创建文件对象   创建HSSFWorkbook只能够写出为xls格式的Excel
//        要写出 xlsx 需要创建为 XSSFWorkbook 两种Api基本使用方式一样
        HSSFWorkbook workbook = new HSSFWorkbook();

//        2.创建表对象
        HSSFSheet sheet = workbook.createSheet(sheetName);

//        3.创建标题栏（第一行）  参数为行下标  行下标从0开始
        HSSFRow titleRow = sheet.createRow(0);

//        4.在标题栏中写入数据
        for (int i = 0; i < titles.length; i++) {
//            创建单元格
            HSSFCell cell = titleRow.createCell(i);
            cell.setCellValue(titles[i]);
        }
        /**
         * 写入用户数据
         */
        List<User> users = new User().build();
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            // 创建行，第0行是标题
            HSSFRow row = sheet.createRow(i + 1);
            // 设置每行上单元格
            row.createCell(0).setCellValue(user.getUserId());
            row.createCell(1).setCellValue(user.getUserName());
            row.createCell(2).setCellValue(user.getNickName());
            row.createCell(3).setCellValue(user.getUserType());
            row.createCell(4).setCellValue(user.getEmail());
        }

        try (FileOutputStream fis = new FileOutputStream("F:/test.xls")){
            workbook.write(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    static class User{
        private Integer userId;
        private String userName;
        private String nickName;
        private String userType;
        private String email;
        public User(){}

        public User(Integer userId, String userName, String nickName, String userType, String email){
            this.email = email;
            this.nickName = nickName;
            this.userId  = userId;
            this.userName = userName;
            this.userType = userType;
        }

        public List<User> build(){
            List<User> list = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                list.add(new User(i, "name" + i, "nick" + i, "1", "email" + i));
            }
            return list;
        }

        public Integer getUserId() {
            return userId;
        }

        public void setUserId(Integer userId) {
            this.userId = userId;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getNickName() {
            return nickName;
        }

        public void setNickName(String nickName) {
            this.nickName = nickName;
        }

        public String getUserType() {
            return userType;
        }

        public void setUserType(String userType) {
            this.userType = userType;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }
    }
}
